// ObjectDB for Java Demo - JDO Directory - A Brief JDO Tutorial
// Copyright (C) 2001-2003, ObjectDB Software. All rights reserved.

package utilities;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Iterator;
import java.util.Properties;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;

/**
 * General JDO Utilities
 */
public class Utilities {
    
    private PersistenceManager pm = null;
    
	public static PersistenceManager getPersistenceManager(boolean clean) {
		try {
			// Prepare the jdo.properties file for reading:
			InputStream in = Utilities.class
					.getResourceAsStream("jdo.properties");

			try {
				// Load the properties from the file:
				Properties properties = new Properties();
				properties.load(in);

				// If requested - try to delete an old local odb file:
				if (clean) {
					String path = (String) properties
							.get("javax.jdo.option.ConnectionURL");
					if (!path.startsWith("objectdb://")) // only local
						new File(path).delete();
				}

				// Obtain a PersistenceManagerFactory and a PersistenceManager:
				PersistenceManagerFactory pmf = JDOHelper
						.getPersistenceManagerFactory(properties,
								JDOHelper.class.getClassLoader());
				return pmf.getPersistenceManager();
			} finally {
				in.close();
			}
		}

		// Handle errors:
		catch (IOException x) {
			throw new RuntimeException("Error reading jdo.properties");
		}
	}

	public static void enhance() {
		String pacotesPc = "br.usp.ime.protoc.genotipagem.*"
				+ ", br.usp.ime.protoc.neuroimagem.*"
				+ ", br.usp.ime.protoc.pessoa.*";
		com.objectdb.Enhancer.enhance(pacotesPc);
		
	    // Obtain a PersistenceManager instance:
	    PersistenceManager pm = getPersistenceManager(true);		
	}

	public static void consultAll(PersistenceManager pm, Class c) {
		pm.currentTransaction().begin();
		System.out.println(" =============== ");
		Query query = pm.newQuery(c);
		Collection results = (Collection) query.execute();
		Iterator itr = results.iterator();
		while (itr.hasNext()) {
			Object obj = (Object) itr.next();
			System.out.println(obj.toString());
		}
		query.close(results);
		System.out.println(" --------------- ");
		pm.currentTransaction().commit();
	}

	public static void deleteAll(PersistenceManager pm, Class c) {
		// Limpa do banco de dados o objeto Raiz Pessoas.
		Query query = null;
		Collection results = null;
		try {
			query = pm.newQuery(c);
			results = (Collection) query.execute();
			Iterator itr = results.iterator();
			while (itr.hasNext()) {
				Object obj = (Object) itr.next();
				if (obj == null)
					break;
				pm.currentTransaction().begin();
				pm.deletePersistent(obj);
				pm.currentTransaction().commit();
				results = (Collection) query.execute();
				itr = results.iterator();
			}
		} catch (Exception e) {
			e.printStackTrace();

		} finally {
			// Fecha query.
			query.close(results);

			if (pm.currentTransaction().isActive()) {
				pm.currentTransaction().rollback();
			}
		}

	}
}
